package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.ArticleReplyStatus;
import com.example.forum.common.enums.ArticleStatus;
import com.example.forum.common.enums.ResultCode;
import com.example.forum.common.enums.UserStatus;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hanzishuai
 * @date 2025/4/12 13:11
 * @Description 回复接口
 */
@Tag(name = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;


    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(description = "帖子 Id") @RequestParam("articleId") @NonNull Long articleId,
                            @Parameter(description = "帖子内容") @RequestParam("content") @NonNull String content) {

        // 获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 判断用户是否被禁言
        if (user.getState() == UserStatus.IS_BANNED.getCode()) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取要回复帖子的帖子对象
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == ArticleStatus.IS_DELETE.getStatus()) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 判断帖子是否被封
        if (article.getState() == ArticleStatus.IS_BANNED.getStatus()) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        // 构建对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId()); // 回复的发送者
        articleReply.setContent(content);

        // 调用 Service
        articleReplyService.creat(articleReply);

        // 返回
        return AppResult.success();
    }


    @Operation(summary = "获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@Parameter(description = "帖子Id") @RequestParam("articleId") @NonNull Long articleId) {

        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == ArticleStatus.IS_DELETE.getStatus()) {
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }

        // 调用 Service
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);

        return AppResult.success(articleReplies);
    }

    @Operation(summary = "删除帖子回复")
    @PostMapping("/delete")
    public AppResult delete(HttpServletRequest request,
                            @Parameter(description = "帖子回复 Id") @RequestParam("articleReplyId") @NonNull Long articleReplyId) {
        // 获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 判断用户是否被禁言
        if (user.getState() == UserStatus.IS_BANNED.getCode()) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        ArticleReply articleReply = articleReplyService.selectByArticleReplyId(articleReplyId);

        if (articleReply == null || articleReply.getDeleteState() == ArticleReplyStatus.IS_DELETE.getStatus()) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_REPLY_NOT_EXISTS.getMessage());
        }

        articleReplyService.delete(articleReplyId);

        return AppResult.success();
    }


}




































